from flask import Blueprint, request, jsonify
from ..controllers.admin import create_admin, get_admins, update_admin, delete_admin, login_admin

admin_bp = Blueprint('admin', __name__, url_prefix='/admin')


@admin_bp.route('/add', methods=['POST'])
def create_admin_route():
    """创建管理员"""
    try:
        data = request.get_json() or {}
        return create_admin(data)
    except Exception as e:
        return jsonify({'code': 500, 'message': '服务器内部错误', 'data': str(e)})


@admin_bp.route('/list', methods=['GET'])
def get_admins_route():
    """获取管理员列表"""
    try:
        data = request.args.to_dict() or {}
        return get_admins(data)
    except Exception as e:
        return jsonify({'code': 500, 'message': '服务器内部错误', 'data': str(e)})


@admin_bp.route('/update', methods=['POST'])
def update_admin_route():
    """更新管理员信息"""
    try:
        data = request.get_json() or {}
        return update_admin(data)
    except Exception as e:
        return jsonify({'code': 500, 'message': '服务器内部错误', 'data': str(e)})


@admin_bp.route('/delete', methods=['POST'])
def delete_admin_route():
    """删除管理员"""
    try:
        data = request.get_json() or {}
        return delete_admin(data)
    except Exception as e:
        return jsonify({'code': 500, 'message': '服务器内部错误', 'data': str(e)})

# 管理员注销登录
@admin_bp.route('/logout', methods=['POST'])
def logout_admin_route():
    """管理员注销登录"""
    return jsonify({'code': 200, 'message': '管理员注销登录成功'})

# 管理员登录
@admin_bp.route('/login', methods=['POST'])
def login_admin_route():
    """管理员登录"""
    try:
        data = request.get_json() or {}
        return login_admin(data)
    except Exception as e:
        return jsonify({'code': 500, 'message': '服务器内部错误', 'data': str(e)})